<!-- 投资机构信息查询组件 -->
<template>
  <div class="skyEyeCheckAuto">
    <el-autocomplete
      v-model="svalue"
      :fetch-suggestions="companyQuerySearch"
      :placeholder="$t(placeholder)"
      :trigger-on-focus="false"
      :maxlength="100"
      show-word-limit
      :value="svalue"
      @select="companyHandleSelect"
    >
      <template v-if="queryButton && enableTyc" slot="append">
        <a @click="companyHandleSelect(tyc)">{{ $t('查询工商信息') }}</a>
      </template>
    </el-autocomplete>
  </div>
</template>

<script>
  import { gpListByName, getGpByName } from '@/api/fund/gp/gpBase'
  import { Notification } from 'element-ui'
  export default {
    name: 'SearchInvestment',
    props: {
      value: {
        type: String,
        default: ''
      },
      placeholder: {
        type: String,
        default: () => '请输入'
      },
      // 是否开启查询按钮
      queryButton: {
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
        // 公司名称
        svalue: '',
        // 返回天眼查信息
        tycData: {},
        // 查询条件
        tyc: {},
        enableTyc: undefined
      }
    },
    watch: {
      value: {
        handler(svalue) {
          if (svalue) {
            this.svalue = svalue
            // 按钮查看条件
            this.tyc = {
              shortName: '',
              name: svalue
            }
          } else {
            this.svalue = ''
            this.tycData = {}
            this.tyc = {}
          }
        },
        deep: true,
        immediate: true
      },
      svalue: {
        handler(svalue) {
          if (svalue) {
            this.svalue = svalue
            // 按钮查看条件
            this.tyc = {
              shortName: '',
              name: svalue
            }
          } else {
            this.svalue = ''
            this.tycData = {}
            this.tyc = {}
          }
          this.$emit('input', this.svalue)
        },
        deep: true,
        immediate: true
      }
    },
    created() {

    },
    mounted() {
       this.enableTyc = (window.localStorage.getItem('enableTyc') === 'true')
    },
    methods: {
      // 标的公司查询
      companyQuerySearch(queryString, cb) {
        gpListByName(queryString).then(res => {
          if (res.code === 200) {
            cb(res.data)
          }
        })
      },
      // 标的公司选择后事件
      companyHandleSelect(item) {
        console.log(item, 'itemitemitemitem')
        if (item.value || item.name) {
          this.svalue = item.value
          if (!this.svalue) {
            // 点击按钮查询时
            this.svalue = item.name
            this.tyc = item
          } else {
            const tyc = {
              // 搜索栏查询时
              name: item.value,
              shortName: item.shortName
            }
            this.tyc = tyc
          }
          this.loading = true
          if (!this.svalue) {
            Notification.error({
              title: this.$t('请先输入公司名称！')
            })
            this.loading = false
            return
          }
          getGpByName(this.tyc).then(res => {
            if (res.code === 200) {
              if (res.msg === 'nofind') {
                Notification.error({
                  title: this.$t('未获取到信息！')
                })
                this.loading = false
              } else {
                this.msgSuccess(this.$t('已获取信息！'))
                this.tycData = res.data
                this.loading = false
              }
              this.$emit('change', res.data)
              this.$emit('input', this.svalue)
            }
          })
        } else {
          this.$emit('change', null)
          this.$emit('input', this.svalue)
          Notification.error({
            title: this.$t('未获取到信息！')
          })
        }
      }
    }
  }
</script>

<style scoped>

</style>
